package de.tabuma.projectview;

import java.util.*;
import java.util.logging.*;
import javax.servlet.http.*;
import de.tabuma.pm.project.entity.*;
import de.tabuma.pm.security.*;
import de.tabuma.shopping.logik.LicenseUseFacade;
import de.tabuma.ssoclient.entity.Attribute;
import de.tabuma.ssoclient.logik.*;
import de.tabuma.pm.design.*;

/**
 *
 * @author alefen
 */
public class ProjectFacade {

    private static final Logger log = Logger.getLogger(ProjectFacade.class.getName());

    public static String getProject(HttpServletRequest request, int projectId) {
        String str = "";
        
        try {

            Project project = de.tabuma.pm.project.logik.ProjectFacade.get(projectId);
            List<SecurityProviderInstance> spiList = de.tabuma.pm.security.SecurityProviderInstanceFacade.getList(projectId);
            SecurityProviderInstance spi = null;
            for(int i=0;i<spiList.size();i++) {
                SecurityProviderInstance spiListElement = spiList.get(i);
                if(spiListElement.getActiv().booleanValue()) {
                    spi = spiListElement;
                    i = spiList.size();
                }
            }

            int setDesignId = 0;
            if(request.getParameter(constants.changeDesign)!=null) {
                setDesignId = Integer.valueOf((String) request.getParameter(constants.changeDesign)).intValue();
            }


            if(request.getParameter("LoginParam")==null && spi!=null) {
                str = showLogin(project, null);
            }
            else {

                String username = request.getParameter("Username");
                String password = request.getParameter("Password");
                List<Attribute> aList = null;
                String sso = null;

                if(spi!=null) {

                    try {
                        sso = SSOFacade.getSSO(username, password);
                        if(!sso.equals("")) {
                            aList = AttributeFacade.getList1(sso);
                        }
                    }
                    catch(Exception e) {
                        str = showLogin(project, "Username or Password is wrong.");
                        return str;
                    }
                }

                List<Design> dList = DesignFacade.getList(projectId);

                long designId = 0;
                if(aList!=null) {
                    for(int i=0;i<aList.size();i++) {
                        Attribute attribute = aList.get(i);
                        if(attribute.getName().equals(constants.tabumaplattformDesignId)) {
                            designId = Long.valueOf(attribute.getValue()).intValue();
                            if(setDesignId!=0) {
                                designId = setDesignId;
                                attribute.setValue(""+setDesignId);
                                AttributeFacade.update(attribute, sso);
                            }
                        }
                    }
                }
                
                designId = setDesignId;

                Design design = null;
                if(designId!=0) {
                    for(int i=0;i<dList.size();i++) {
                        Design d = dList.get(i);
                        if(designId==d.getId().longValue()) {
                            design = d;
                        }
                    }
                }
                else {
                    if(dList.size()>0) {
                        design = dList.get(0);
                    }
                }

                if(design==null) {
                    design = new Design();
                    design.setCssUrl("css/ribbon.css");
                    design.setDesignType(DesignType.TYPE_RIBBON_MENU);
                    design.setName("RibbonDesign");
                    design.setProjectId(projectId);
                }

                try {
                    if(spi!=null) {
                        LicenseUseFacade.create(project.getId().intValue(), "Login " + username, de.tabuma.pm.constants.PM_SHOPPING_VIEW_PROTECTED_LICENSE, de.tabuma.pm.constants.PM_SHOPPING_VIEW_PROTECTED_LICENSE, username);
                    }
                    else {
                        LicenseUseFacade.create(project.getId().intValue(), "Login " + username, de.tabuma.pm.constants.PM_SHOPPING_VIEW_PUBLIC_LICENSE, de.tabuma.pm.constants.PM_SHOPPING_VIEW_PUBLIC_LICENSE, username);
                    }
                }
                catch(Exception e) {
                    String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
                    for(int i=0;i<e.getStackTrace().length;i++) {
                        warning += e.getStackTrace()[i]+"\n";
                    }
                    log.warning(warning);
                    str += "License needed.";
                    return str;
                }

                switch(design.getDesignType()) {
                    case DesignType.TYPE_ACCORDING_MENU:
                        str = de.tabuma.projectview.according.AccodingFacade.getProjectString(request, project, aList, sso, design);
                        break;
                    case DesignType.TYPE_CSS_DROP_DOWN_MENU:
                        str = de.tabuma.projectview.cssdropdown.CSSDropDownFacade.getProjectString(request, project, aList, sso, design);
                        break;
                    case DesignType.TYPE_TAB_MENU:
                        str = de.tabuma.projectview.cssdropdown.CSSDropDownFacade.getProjectString(request, project, aList, sso, design);
                        break;
                    case DesignType.TYPE_NAVBAR:
                        str = de.tabuma.projectview.navbar.NavbarFacade.getProjectString(request, project, aList, sso, design);
                        break;
                    default:
                        str = de.tabuma.projectview.ribbon.RibbonFacade.getProjectString(request, project, aList, sso, design);
                        break;
                }
            }
        }
        catch(Exception e) {
            str += "General error.";
            String warning = "";
            warning += e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        return str;
    }

    public static String showLogin(Project project, String message) {
        String str = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\n";
        str += "<html>\n";
        str += "\t<head>\n";
        str += "\t\t<title>"+project.getName()+" Login</title>\n";
        str += "\t\t<link rel=\"stylesheet\" href=\"css/ribbon.css\" type=\"text/css\">\n";
        str += "\t</head>\n";
        str += "\t<body>\n";
        str += "\t\t<form action=\"projectview\" method=\"post\">\n";
        if(message!=null) {
            str += "\t\t\t<div>"+message+"</div>\n";
        }
        str += "\t\t\t<div class=\"login\">\n";
        str += "\t\t\t\t<div class=\"login1\">\n";
        str += "\t\t\t\t\t<div class=\"loginheader\">Login into "+project.getName()+"</div>\n";
        str += "\t\t\t\t\t<div class=\"logincontent\">\n";
        str += "\t\t\t\t\t\t<input type=\"hidden\" name=\"LoginParam\" value=\"true\"/>\n";
        str += "\t\t\t\t\t\t<input type=\"hidden\" name=\"projectId\" value=\""+project.getId()+"\"/>\n";
        str += "\t\t\t\t\t\t<div class=\"formline\">\n";
        str += "\t\t\t\t\t\t\t<div class=\"formline1\">Username:</div>\n";
        str += "\t\t\t\t\t\t\t<div class=\"formline2\"><input type=\"text\" name=\"Username\" value=\"\"/></div>\n";
        str += "\t\t\t\t\t</div>\n";
        str += "\t\t\t\t\t\t<div class=\"formline\">\n";
        str += "\t\t\t\t\t\t\t<div class=\"formline1\">Password:</div>\n";
        str += "\t\t\t\t\t\t\t<div class=\"formline2\"><input type=\"password\" name=\"Password\" value=\"\"/></div>\n";
        str += "\t\t\t\t\t\t</div>\n";
        str += "\t\t\t\t\t\t<div class=\"formline\">\n";
        str += "\t\t\t\t\t\t\t<input type=\"submit\" name=\"Login\" value=\"Login\"/>\n";
        str += "\t\t\t\t\t</div>\n";
        str += "\t\t\t\t\t</div>\n";
        str += "\t\t\t\t</div>\n";
        str += "\t\t\t</div>\n";
        str += "\t\t</form>\n";
        str += "\t</body>\n";
        str += "</html>\n";
        return str;
    }
}
